package com.pap.product.calculate.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.pap.base.controller.BaseController;
import com.pap.base.util.jackson.JacksonUtilss;
import com.pap.obj.vo.response.ResponseVO;
import com.pap.product.agent.calculate.IProductCalculateAgent;
import com.pap.product.agent.calculate.dto.ProductCalculateDTO;
import com.pap.product.agent.calculate.dto.check.ProductCalculateCheckDTO;
import com.pap.product.agent.calculate.dto.check.ProductCalculateResponseDTO;
import com.pap.product.calculate.auto.entity.ProductCalculate;

@RestController
@RequestMapping("/v1/product/calculate")
public class ProductCalculateController extends BaseController<ProductCalculate, IProductCalculateAgent> {

	private static Logger logger  =  LoggerFactory.getLogger(ProductCalculateController.class);
	
	@Resource(name = "productCalculateAgent")
	@Override
	public void setService(IProductCalculateAgent agent) {
		super.service = agent;
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/saveorupdate")
	public ResponseVO<ProductCalculate> save(@RequestHeader(value = "clientLicenseId", defaultValue = "-1") String clientLicenseId,
			@RequestHeader(value = "loginUserId", defaultValue = "-1") String loginUserId,
			@RequestParam(value = "operJSON", required = true) String operJSON) throws Exception {

		ObjectMapper objectMapper = JacksonUtilss.getObjectMapper();
		ProductCalculateDTO e = objectMapper.readValue(operJSON, ProductCalculateDTO.class);

		int operationInt = service.saveOrUpdateCalculator(e, clientLicenseId, loginUserId);
		return ResponseVO.successdata(operationInt);
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/calculate")
	public ResponseVO<ProductCalculate> calculate(@RequestHeader(value = "clientLicenseId", defaultValue = "-1") String clientLicenseId,
			@RequestHeader(value = "loginUserId", defaultValue = "-1") String loginUserId,
			@RequestParam(value = "productId", required = true) String productId,
			@RequestParam(value = "calculateJSON", required = true) String calculateJSON) throws Exception {

		ObjectMapper objectMapper = JacksonUtilss.getObjectMapper();
		JavaType javaType = getCollectionType(objectMapper, ArrayList.class, ProductCalculateCheckDTO.class); 
		List<ProductCalculateCheckDTO> inputCalculateList = objectMapper.readValue(calculateJSON, javaType);

		List<ProductCalculateResponseDTO> outputCalculateList = service.calculate(productId, inputCalculateList, clientLicenseId, loginUserId);
		return ResponseVO.successdatas(outputCalculateList, null);
	}
	
    /**   
     * 获取泛型的Collection Type  
     * @param collectionClass 泛型的Collection   
     * @param elementClasses 元素类   
     * @return JavaType Java类型   
     * @since 1.0   
     */   
	public static JavaType getCollectionType(ObjectMapper mapper, Class<?> collectionClass, Class<?>... elementClasses) {   
		return mapper.getTypeFactory().constructParametricType(collectionClass, elementClasses);   
	}
}
